package com.web.itemService.utils;

import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsResponse;
import com.google.gson.Gson;
import com.web.itemService.properties.AliyuProperty;
import darabonba.core.client.ClientOverrideConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

@Component
public class sendMessage {
    @Autowired
    AliyuProperty aliyuProperty;

    public void sendCodeMessage(int code,String phoneNumber) throws ExecutionException, InterruptedException {
        {




            StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                    // Please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set.
                    .accessKeyId(aliyuProperty.getAccessKeyId()).accessKeySecret(aliyuProperty.getAccessKeySecret())
                    //.securityToken(System.getenv("ALIBABA_CLOUD_SECURITY_TOKEN")) // use STS token
                    .build());


            AsyncClient client = AsyncClient.builder().region("cn-qingdao")

                    .credentialsProvider(provider)

                    .overrideConfiguration(ClientOverrideConfiguration.create()

                                    .setEndpointOverride("dysmsapi.aliyuncs.com")

                    ).build();


            SendSmsRequest sendSmsRequest = SendSmsRequest.builder().signName("高洋学习").phoneNumbers(phoneNumber).templateCode("SMS_468210524").templateParam(String.format("{\"code\":\"%d\"}",code))

                    .build();


            CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);

            SendSmsResponse resp = response.get();
            System.out.println(new Gson().toJson(resp));

            client.close();
        }
    }
}
